package com.qiyun.dinghuohui.pop;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qiyun.dinghuohui.R;
import com.qiyun.dinghuohui.base.BasePopWindow;
import com.qiyun.dinghuohui.utils.CommonUtil;
import com.qiyun.dinghuohui.utils.Inject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xlm on 2018/6/3
 */

public class ThSearchChangePop extends BasePopWindow {
    public ThSearchChangePop(Context context) {
        super(context);
    }

    @Override
    public void onSetAttr() {
        setWidth(CommonUtil.dip2px(90, getContext()));
        setHeight(CommonUtil.dip2px(60, getContext()));
    }

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.pop_sxchange);
    }

    @Inject(R.id.pop_sx_recycle)
    private RecyclerView mRecycleSx;

    @Override
    protected void initViews() {

    }

    @Override
    protected void onSetListener() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<SxBean> list = adapter.getData();
                for (int i = 0; i < list.size(); i++) {
                    if (i == position) {
                        list.get(i).setSelected(true);
                    } else {
                        list.get(i).setSelected(false);
                    }
                }
                mAdapter.setNewData(list);
                if (listener != null) {
                    listener.onSelected(list.get(position).getFilter());
                }
                dismiss();
            }
        });
    }

    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public static class Listener {
        public void onSelected(String filter) {
        }
    }

    private ArrayList<SxBean> mNums;
    private Adapter mAdapter;

    @Override
    protected void afterOnCreate() {
        mNums = new ArrayList<>();
        String[] filters = new String[]{"搭配号", "组别"};
        for (int i = 0; i < filters.length; i++) {
            SxBean sx = new SxBean();
            if (i == 0) {
                sx.setSelected(true);
            } else {
                sx.setSelected(false);
            }
            sx.setFilter(filters[i]);
            mNums.add(sx);
        }
        mAdapter = new Adapter(R.layout.item_pop_search, mNums);
        mRecycleSx.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mRecycleSx.setAdapter(mAdapter);
    }

    private class Adapter extends BaseQuickAdapter<SxBean, BaseViewHolder> {

        public Adapter(@LayoutRes int layoutResId, @Nullable List<SxBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, SxBean item) {
            helper.setText(R.id.sx_txt, item.getFilter());
            helper.setTextColor(R.id.sx_txt, item.isSelected() ?
                    Color.parseColor("#FF7170") : Color.parseColor("#333333"));
        }
    }

    private class SxBean implements Serializable {
        private boolean isSelected;
        private String filter;

        public boolean isSelected() {
            return isSelected;
        }

        public void setSelected(boolean selected) {
            isSelected = selected;
        }

        public String getFilter() {
            return filter;
        }

        public void setFilter(String filter) {
            this.filter = filter;
        }
    }
}
